package info.ask4research.dao.impl;

import info.ask4research.dao.IAddonDAO;
import info.ask4research.dao.core.impl.AbstractDaoImpl;
import info.ask4research.model.addons.Addon;
import info.ask4research.model.addons.MultipleChoice;
import info.ask4research.model.addons.QuestionsAndAnswers;
import info.ask4research.model.addons.TextWork;
import info.ask4research.model.addons.UploadingFiles;
import info.ask4research.model.roles.Role;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("addonDAO")
public class AddonDAO extends AbstractDaoImpl<Addon> implements IAddonDAO {

	public AddonDAO() {
		super(Addon.class);
	}
	
	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public boolean textWorkExists(String text, Integer unitOfLearningId) {
		List<TextWork> roles = (List<TextWork>) hibernateTemplate.find("from TextWork textWork where textWork.unitOfLearning.id=? and textWork.title=?",  unitOfLearningId,text);
		if(roles.isEmpty()){
			return false;
		}else{
			return true;
		}
	}
	
	@Override
	public boolean uploadingFilesExists(String text, Integer unitOfLearningId) {
		List<UploadingFiles> roles = (List<UploadingFiles>) hibernateTemplate.find("from UploadingFiles uploadingFiles where uploadingFiles.unitOfLearning.id=? and uploadingFiles.title=?",  unitOfLearningId,text);
		if(roles.isEmpty()){
			return false;
		}else{
			return true;
		}
	}
	
	@Override
	public boolean qnrExists(String text, Integer unitOfLearningId) {
		List<QuestionsAndAnswers> roles = (List<QuestionsAndAnswers>) hibernateTemplate.find("from QuestionsAndAnswers questionsAndAnswers where questionsAndAnswers.unitOfLearning.id=? and questionsAndAnswers.title=?",  unitOfLearningId,text);
		if(roles.isEmpty()){
			return false;
		}else{
			return true;
		}
	}
	
	@Override
	public boolean mctExists(String text, Integer unitOfLearningId) {
		List<MultipleChoice> roles = (List<MultipleChoice>) hibernateTemplate.find("from MultipleChoice multipleChoice where multipleChoice.unitOfLearning.id=? and multipleChoice.title=?",  unitOfLearningId,text);
		if(roles.isEmpty()){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public List<TextWork> getTextWorkAddonByUoL(int id) {
		return (List<TextWork>) hibernateTemplate.find("from TextWork textWork where textWork.unitOfLearning.id=?",  id);
	}

	@Override
	public List<UploadingFiles> getUploadingFilesAddonByUoL(int id) {
		return (List<UploadingFiles>) hibernateTemplate.find("from UploadingFiles uploadingFiles where uploadingFiles.unitOfLearning.id=?",  id);
	}

	@Override
	public List<QuestionsAndAnswers> getQuestionsAnswersAddonByUoL(int id) {
		return (List<QuestionsAndAnswers>) hibernateTemplate.find("from QuestionsAndAnswers questionsAndAnswers where questionsAndAnswers.unitOfLearning.id=?",  id);
	}

	@Override
	public List<MultipleChoice> getMcTestAddonByUoL(int id) {
		return (List<MultipleChoice>) hibernateTemplate.find("from MultipleChoice multipleChoice where multipleChoice.unitOfLearning.id=?",  id);
	}

	@Override
	public Addon getById(Integer qnrId) {
		return hibernateTemplate.get(Addon.class, qnrId);
	}

	@Override
	public boolean addonNameExists(String stringAddon, Integer uoLId) {
		List<Addon> addons = (List<Addon>) hibernateTemplate.find("from Addon addon where addon.unitOfLearning.id=? and addon.title=?",  uoLId,stringAddon);
		if(addons.isEmpty()){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public Addon getAddonByUoFandRoleName(String stringAddon, Integer uoLId) {
		List<Addon> addons = (List<Addon>) hibernateTemplate.find("from Addon addon where addon.unitOfLearning.id=? and addon.title=?",  uoLId,stringAddon);
		return addons.get(0);
	}
	
	@Override
	public List<Addon> getAddonsByUoL(int id) {
		List<Addon> addons = (List<Addon>) hibernateTemplate.find("from Addon addon where addon.unitOfLearning.id=?",  id);
		return addons;
	}
}
